package com.alex.http;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.util.Log;

public class AHttpEngine implements Runnable{

	public String TAG = "AHttpEngine";
	private static AHttpEngine instance;
	private Queue<AHttpRequest> mQueue;
	private ExecutorService mHttpExecutor;
	
	public static AHttpEngine getInstance(){
		if(instance == null){
			instance = new AHttpEngine();
		}
		return instance;
	}
	
	private AHttpEngine(){
		mHttpExecutor =Executors.newCachedThreadPool(); 
		mQueue = new LinkedList<AHttpRequest>();
		Thread t = new Thread(this);
		t.start();
		//Future<?> futureTask = mHttpExecutor.submit();
	}
	
	public void request(AHttpRequest request){
		Log.d(TAG, "request");
		mQueue.add(request);
	}

	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			if(this.mQueue.peek() != null) {
				AHttpRequest httpRequest = (AHttpRequest)this.mQueue.remove();
				mHttpExecutor.submit(httpRequest);
			}
		}
	}
	
	
}
